package com.hurl.waguji.biz.download;

import org.hibernate.ScrollableResults;
import org.hibernate.criterion.Restrictions;

import com.hurl.library.spider.MultiUrlPattern;
import com.hurl.waguji.persistence.DmGpBO;

abstract class StockIteratorUrlPattern implements MultiUrlPattern{

	private boolean started = false;
	private ScrollableResults results;
	private DownloadContext context;
	public StockIteratorUrlPattern(DownloadContext context){
		this.context=context;
	}
	
	public abstract String bo2url(DmGpBO bo);
	
	private void start(){
		if(started){
			return;
		}
		results = context.getDao().createCriteria(DmGpBO.class).add(Restrictions.eq("flag", 'Y')).setReadOnly(true).scroll();
		started=true;
	}
	@Override
	public String nextUrl() {
		start();
		if(results.next()){
			DmGpBO bo = (DmGpBO)results.get(0);
			context.setEnv("gpdm", bo.getGpDm());
			return bo2url(bo);
		}else{
			return null;
		}
	}

}
